<?php

namespace Xuan\Controller;

use Think\Controller;

class PublicController extends Controller {

    public function login() {// 用户登录页面
        $s_admin_uid = session("admin_uid") ? session("admin_uid") : cookie("admin_uid");
        if ($s_admin_uid != '') {
            echo "<script>document.location.href='" . __APP__ . "/" . MODULE_NAME . "';</script>";
        }
        $name = I('post.name');
        if ($name == '' && I('post.pwd') == '') {
            
        } else {
            if (I('post.pwd') == '') {
                $error = '密码一栏为空';
            } else {
                $pwd = md5(I('post.pwd'));
                $info = M("admin_user")->field("id,action")->where("name='" . $name . "' AND pwd = '" . $pwd . "'")->find();
                if ($info) {
                    if (I("post.rememberme") == '1') {//记住密码
                        cookie("admin_uid", $info['id']);
                        cookie("admin_name", $name);
                    }
                    session('admin_uid', $info['id']);
                    session('admin_name', $name);
                    M("admin_user")->where("id = " . $info['id'] . "")->save(array("last_login" => time(), "last_ip" => getIP()));
                    if ($info['action']) {
                        $menu = M("admin_menu2")->field("control,mod")->where("id in (" . $info['action'] . ") AND pid !=0 AND control !=''")->find();
                    }
//                    echo M("admin_menu2")->getlastsql();
                    if ($info['id'] == 1) {
                        $url = __APP__. "/" . MODULE_NAME;
                    } else {
                        $url = U($menu['control'] . "/" . $menu['mod']);
                    }
                    echo "<script>document.location.href='" . $url . "';</script>";
                } else {
                    $error = '无效密码。<a title="找回丢失的密码" href="">忘记密码</a>？';
                }
            }
        }

        $this->assign("error", $error);
        $this->assign("config", getTableConfig());
        $this->display();
    }

    public function logout() {// 用户登出
        session('admin_uid', null);
        cookie('admin_uid', null);
        $this->redirect("Public/login");
    }

}